/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MVC;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;

/**
 * BidirectionalMap
 * Class specific for building the bidirectional HashMap
 * An address IP corresponds with a name of user
 * @author Duy Duc, Metangmo
 */
public class BidirectionalMap{
        private Map<InetAddress, String> keyToValueMap = new HashMap<InetAddress, String>();
        private Map<String, InetAddress> valueToKeyMap = new HashMap<String, InetAddress>();
        
        /**
         * put:
         * function to associate the specified value with the specified key in this map in both directions
         * @param key
         *        IP Address of a user
         *        type InetAddress
         * @param value
         *        Nickname of the user corresponds with the IP address
         *        type String
         */
        synchronized public void put(InetAddress key, String value){
            keyToValueMap.put(key, value);
            valueToKeyMap.put(value, key);
        }
        
        /**
         * removeByKey:
         * function to remove the mapping value for the specified key from this map if present
         * @param key
         *        IP Address of a user
         *        type InetAddress
         * @return 
         *        Nickname of the user removed corresponds with the IP address
         *        type String
         */
        synchronized public String removeByKey(InetAddress key){
            String removedValue = keyToValueMap.remove(key);
            valueToKeyMap.remove(removedValue);
            return removedValue;
        }
        
        /**
         * removeByValue:
         * function to remove the mapping key for the specified value from this map if present
         * @param value
         *        Nickname of a user
         *        type String
         * @return 
         *        IP Address of the user removed corresponds with the name
         *        type InetAddress
         */
        
        synchronized public InetAddress removeByValue(String value){
            InetAddress removedKey = valueToKeyMap.remove(value);
            keyToValueMap.remove(removedKey);
            return removedKey;
        }
        
        /**
         * containsKey:
         * function returns true if this map contains a mapping for the specified key.
         * @param key
         *        IP Address of a user
         *        type InetAddress
         * @return 
         *        True if a mapping for the specified key exists
         */
        
        public boolean containsKey(InetAddress key){
            return keyToValueMap.containsKey(key);
        }
        
        /**
         * containsValue:
         * function returns true if this map contains a mapping for the specified value.
         * @param value
         *        Nickname of a user
         *        type String
         * @return 
         *        True if a mapping for the specified value exists
         */
        public boolean containsValue(InetAddress value){
            return keyToValueMap.containsValue(value);
        }
        
        /**
         * getKey:
         * function returns the key to which the specified value is mapped, or null if this map contains no mapping for the value
         * @param value
         *        Nickname of a user
         *        type String
         * @return 
         *        IP Address of the user mapped with the specified value if the mapping exists
         *        type InetAddress
         */
        public InetAddress getKey(String value){
            return valueToKeyMap.get(value);
        }
        
        /**
         * getValue:
         * function returns the value to which the specified key is mapped, or null if this map contains no mapping for the key
         * @param key
         *        IP Address of a user
         *        type InetAddress
         * @return 
         *        Nickname of the user mapped with the specified key if the mapping exists
         *        type String  
         */
        
        public String getValue(InetAddress key){
            return keyToValueMap.get(key);
        }
}
